<template>
  <div id="login">
    <vue-particles color="#EAB15B"
                   :particleOpacity="0.7"
                   :particlesNumber="60"
                   shapeType="circle"
                   :particleSize="4"
                   linesColor="#EAB15B"
                   :linesWidth="1"
                   :lineLinked="true"
                   :lineOpacity="0.4"
                   :linesDistance="150"
                   :moveSpeed="2"
                   :hoverEffect="true"
                   hoverMode="grab"
                   :clickEffect="true">
    </vue-particles>
    <div class="login-view">
      <div class="title">FNIU博客后台</div>
      <div class="form">
        <a-form-model :model="formInline"
                      @submit="handleSubmit"
                      @submit.native.prevent>
          <a-form-model-item>
            <a-input v-model="formInline.mobile"
                     placeholder="请输入手机号">
              <a-icon slot="prefix"
                      type="user"
                      style="color: rgba(0, 0, 0, 0.25)" />
            </a-input>
          </a-form-model-item>
          <a-form-model-item>
            <a-input v-model="formInline.password"
                     type="password"
                     placeholder="请输入密码">
              <a-icon slot="prefix"
                      type="lock"
                      style="color: rgba(0, 0, 0, 0.25)" />
            </a-input>
          </a-form-model-item>
          <a-form-model-item class="login-btn">
            <a-button type="primary"
                      html-type="submit"
                      :disabled="formInline.user === '' || formInline.password === ''"> Log in </a-button>
          </a-form-model-item>
        </a-form-model>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        mobile: '',
        password: '',
      },
    }
  },
  created() {
    let mobile = localStorage.getItem('_mobile')
    mobile && (this.formInline.mobile = mobile)
  },
  methods: {
    handleSubmit() {
      this.$api.login(this.formInline).then((res) => {
        if(res.code === 1 && res.data.access_token) {
          localStorage.setItem('_token', res.data.access_token)
          localStorage.setItem('_mobile', this.formInline.mobile)
          this.$router.replace('/')
        } else {
          this.$message.error(res.message || '错误')
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
#login {
  width: 100%;
  height: 100%;
  position: relative;

  #particles-js {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
  }

  .login-view {
    width: 550px;
    height: 400px;
    position: absolute;
    z-index: 5;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 30px;
    box-shadow: 0 0 6px #ccc;
    border-radius: 10px;

    .title {
      font-size: 30px;
      text-align: center;
      margin: 40px 0 50px;
      color: #e6a23c;
    }

    .form {
      width: 300px;
      margin: auto;
    }

    .login-btn {
      margin-top: 30px;
    }

    .ant-btn {
      width: 100%;
    }
  }
}
</style>